package com.chenfeng.back.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.chenfeng.common.controller.BaseConroller;
import com.chenfeng.common.util.ConstantFinalUtil;
import com.chenfeng.common.util.PageInfoUtil;
import com.chenfeng.users.pojo.AAdmins;
import com.chenfeng.users.service.IUserService;
@Controller
@RequestMapping("/back/admins")
public class AdminsBackController extends BaseConroller
{
	@Resource
	IUserService iUserService;
	@RequestMapping("/index.htm")
	//管理员登录后的主页
	public String index()
	{
		return "/back/index";
	}
	@RequestMapping("/logout.htm")
	//管理员退出并销毁session对象
	public String logout(HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		session.removeAttribute("admins");
		session.removeAttribute("lastLoginTime");
		return "redirect:/adminsLogin.htm";
	}
	//管理员左侧菜单显示
	@RequestMapping("/leftMenu.htm")
	public String leftMenu()
	{
		return "back/leftMenu";
	}
	//管理员列表
	@RequestMapping("adminsList.htm")
	public String adminsList(HttpServletRequest request)
	{
		ConstantFinalUtil.LOGGER_SYSTEM.info("adminsList");
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		System.out.println("et:"+st+"sd"+ed);
		if(st == null)
		{
			st ="";
		}
		if(ed == null)
		{
			ed = "";
		}
		Date stDate = null;
		Date edDate = null;
		if(!"".equalsIgnoreCase(st)&&!"".equalsIgnoreCase(ed))
		{
			//将字符串转换成时间
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);		
		}
		//分页查询
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("keyword", keyword);
		map.put("status", status);
		map.put("stDate", stDate);
		map.put("edDate", edDate);
		//查询满足条件的结果
		List<AAdmins> adminsList = this.iUserService.findCondListAdmins(pageInfoUtil, map);
		//存储结果
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		//存储条件
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		ConstantFinalUtil.LOGGER_SYSTEM.info("adminsList{}",adminsList);
		return "/back/adminsList";
	}
	//管理员插入操作
	@RequestMapping("/insert.htm")
	public String insert()
	{	
		return "/back/insert";
	}
	@RequestMapping(value = "/adminsInsertSubmit.htm" ,produces="text/html;charset=utf-8")
	public @ResponseBody String adminsInsertSubmit(AAdmins admins,HttpServletRequest request)
	{
		admins.setPassword(this.encryptUtil.encryString(admins.getPassword()));
		admins.setCreateTime(new Date());
		admins.setLastLoginTime(new Date());
		admins.setUpdateTime(new Date());
		JSONObject json = this.iUserService.insertOneAdmins(admins);
		ConstantFinalUtil.LOGGER_SYSTEM.info("输出结果为{}",json);
		JSONObject returnJson  = this.returnJuiJSON(request, json.getString("info"));
		return returnJson.toJSONString();
	}
}
